package com.esheeper.mainserver.util;

import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ResponseUtil {


    public static final int REQUEST_ERROR_NOT_AUTHORIZATION = 401;
    public static final int REQUEST_ERROR_NOT_ALLOW = 403;
    public static final int REQUEST_ERROR_NOT_FIND_RESOURCE = 404;
    public static final int REQUEST_ERROR_PARAMS_ERROR = 461;
    public static final int RESPONSE_ERROR_SER_ERROR = 500;
    public static Map<Integer,String> error;

    public ResponseUtil(){
        error = new HashMap<Integer,String>();
        error.put(REQUEST_ERROR_NOT_AUTHORIZATION,"未授权");
        error.put(REQUEST_ERROR_NOT_ALLOW, "禁止访问");
        error.put(REQUEST_ERROR_NOT_FIND_RESOURCE,"未找到资源");
        error.put(REQUEST_ERROR_PARAMS_ERROR, "路径参数错误。");
        error.put(RESPONSE_ERROR_SER_ERROR, "服务器未知错误。");
    }

    public Map success(Object data){
        Map map = new HashMap<String,Object>();
        map.put("code","200");
        map.put("msg","ok");
        map.put("data",data);
        return map;
    }

    public Map fail(int code,Object data){
        Map map = new HashMap<String,Object>();
        map.put("code",code);
        String msg = error.get(code);
        map.put("msg",msg== null ?  "未知错误": msg);
        if(data != null)  map.put("data", data);
        return map;
    }
}
